package com.atguigu.schedule.controller;

import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.common.ResultCodeEnum;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.service.impl.SysUserServiceImpl;
import com.atguigu.schedule.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author admin
 */
@WebServlet("/user/*")
public class SysUserController extends BaseController {

    private SysUserService userService = new SysUserServiceImpl();

    /**
     * 校验用户名是否可用
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void checkUsernameUsed (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("username") != null) {
            String username = req.getParameter("username");
            //SysUser sysUser = new SysUser(null, username, null);
            //int rows = userService.register(sysUser);
            SysUser sysUser= userService.findByUserName(username);
            //设置utf-8
            resp.setContentType("text/html;charset=utf-8");

            //封装后，返回
            Result<Object> result = Result.ok(null);

            if (sysUser!=null){
                //ok.setCode(ResultCodeEnum.USERNAME_USED.getCode());
                //ok.setMessage(ResultCodeEnum.USERNAME_USED.getMessage());
                result=Result.build(null, ResultCodeEnum.USERNAME_USED);
                
            }else {
                //ok.setCode(ResultCodeEnum.SUCCESS.getCode());
                //ok.setMessage(ResultCodeEnum.SUCCESS.getMessage());
            }

            /*if (sysUser!=null){
                resp.getWriter().write("用户名已存在");
            }else {
                resp.getWriter().write("用户名可用");
            }*/
            /*if (rows > 0) {
                resp.getWriter().write("用户名已存在");
            } else {
                resp.getWriter().write("用户名可用");
            }*/
        }
    }

    /**
     * 接收用户注册请求的业务处理逻辑（请求接口，非java的interface）
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收用户注册的请求参数
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");
        //2.验证用户注册的请求参数
        //把参数放到对象中

        //3.将用户注册的请求参数保存到数据库
        SysUser sysUser = new SysUser(null, username, userPwd);
        System.out.println("sysUser :"+sysUser);
        int rows = userService.register(sysUser);
        //4.返回响应结果
        if (rows > 0) {
            //resp.getWriter().write("注册成功");
            resp.sendRedirect("/registSuccess.html");
        } else {
            //resp.getWriter().write("注册失败");
            resp.sendRedirect("/registFail.html");
        }
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String userPwd = req.getParameter("userPwd");
        SysUser loginUser=userService.findByUserName(username);
        if(loginUser==null){
            resp.sendRedirect("/loginUsernameError.html");
        }else if (!MD5Util.encrypt(userPwd).equals(loginUser.getUserPwd())){
            resp.sendRedirect("/loginUserPwdError.html");
        }else {

            //将登录成功后的用户信息放到session
            HttpSession session = req.getSession();
            session.setAttribute("sysUser", loginUser);


            resp.sendRedirect("/showSchedule.html");
        }
    }


}
